<template>
  <div class="app-container">
    <data-table
      v-loading="listLoading"
      ref="dataTable"
      :key="tableKey"
      :url="url"
      :border="false"
      :stripe="true"
      row-key="id"
      size="small"
      style="width: 100%;">

      <el-table-column label="编号" prop="id" align="center" width="100px"/>
      <el-table-column label="年级" prop="name" align="center" min-width="100px"/>
      <el-table-column label="学习阶段" prop="stage" align="center" min-width="100px">
        <template slot-scope="{row}">
          {{ row.stage | stageFilter }}
        </template>
      </el-table-column>

      <el-table-column label="是否毕业季" prop="graduateGrade" align="center" >
        <template slot-scope="{row}" >
          <span>{{ row.graduateGrade ?"是":'否' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="下个年级" prop="nextGradeName" align="center" />
      <el-table-column label="排序号" prop="code" align="center" />

      <el-table-column label="是否启用" prop="active" align="center" >
        <template slot-scope="{row}" >
          <span>{{ row.active ?"是":'否' }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="canEdit" label="操作" align="center" width="100px" class-name="small-padding fixed-width" >
        <template slot="header" slot-scope="scope">
          <el-button v-if="canAdd" type="text" size="mini" @click="handleCreate">添加</el-button>
          <template v-else>{{ scope.column.label }}</template>
        </template>
        <template slot-scope="scope" >
          <el-button v-if="canUpdate" type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <confirm-poptip v-if="canDelete" type="text" size="mini" @on-ok="handleDelete(scope.row)"/>
        </template>
      </el-table-column>
    </data-table>

    <el-dialog v-el-drag-dialog :title="dialogFormTitle" :visible.sync="dialogFormVisible" :close-on-click-modal="false" width="400px">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="80px" >

        <el-form-item v-if="!isNew" label="编号" prop="id">
          <el-input v-model="temp.id" :max-length="30" :disabled="true"/>
        </el-form-item>
        <el-form-item label="年级" prop="name">
          <el-input v-model="temp.name" :max-length="20"/>
        </el-form-item>
        <el-form-item label="学习阶段" prop="stage">
          <el-select v-model="temp.stage" filterable >
            <el-option v-for="item in educationStages" :key="item.code" :label="item.name" :value="item.code"/>
          </el-select>
        </el-form-item>
        <el-form-item label="毕业季" prop="graduateGrade">
          <el-switch v-model="temp.graduateGrade" />
        </el-form-item>
        <el-form-item label="下个年级" prop="nextGradeId">
          <el-select v-model="temp.nextGradeId" filterable clearable>
            <el-option v-for="item in grades" :key="item.code" :label="item.name" :value="item.code"/>
          </el-select>
        </el-form-item>
        <el-form-item label="排序号" prop="code">
          <el-input v-model="temp.code" :max-length="20"/>
        </el-form-item>
        <el-form-item label="是否启用" prop="active">
          <el-switch v-model="temp.active" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="isNew?createData():updateData()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getEducationStages, getGrades } from '@/api/public'
import { add, update, remove } from '@/api/grades'
import waves from '@/directive/waves' // 水波纹指令
import elDragDialog from '@/directive/el-dragDialog'
import { canAdd, canUpdate, canDelete } from '@/utils'

const stageKeyValue = {}
export default {
  name: 'Grades',
  directives: {
    waves, elDragDialog
  },
  components: { },
  filters: {
    stageFilter(stage) {
      return stageKeyValue[stage]
    }
  },
  data() {
    return {
      controllerName: 'grades',
      url: 'grades',
      educationStages: [],
      grades: [],
      showDetail: false,
      detailId: null,
      tableKey: 0,
      listLoading: false,
      queryInfo: null,

      defaultForm: {
        id: undefined,
        name: null,
        stage: null,
        graduateGrade: false,
        nextGradeId: null,
        code: null,
        active: false
      },
      temp: {
      },

      rules: {
        name: [{ required: true, message: '不能为空' }],
        stage: [{ required: true, message: '不能为空' }],
        graduateGrade: [{ required: true, message: '不能为空' }],
        code: [{ required: true, message: '不能为空' }]
      },
      dialogFormVisible: false,
      isNew: false
    }
  },

  computed: {
    canEdit() {
      return this.canAdd || this.canUpdate || this.canDelete
    },
    canAdd() {
      return canAdd(this.$store.state, this.controllerName)
    },
    canUpdate() {
      return canUpdate(this.$store.state, this.controllerName)
    },
    canDelete() {
      return canDelete(this.$store.state, this.controllerName)
    },
    dialogFormTitle() {
      return this.isNew ? '新建' : '编辑'
    }
  },
  watch: {
  },
  created() {
    this.getPublicData()
  },
  methods: {

    getPublicData() {
      this.getEducationStages()
      this.getGrades()
    },
    getEducationStages() {
      getEducationStages().then((res) => {
        this.educationStages = res.data
        for (const item of res.data) {
          stageKeyValue[item.code] = item.name
        }
      })
    },

    getGrades() {
      getGrades().then((res) => {
        this.grades = res.data
        for (const item of res.data) {
          item.code = +item.code
        }
      })
    },
    handleQuery() {
      this.$refs.dataTable.setQuery(this.queryInfo)
    },
    resetTemp() {
      this.temp = JSON.parse(JSON.stringify(this.defaultForm))
    },
    handleCreate() {
      this.resetTemp()
      this.isNew = true
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          add(this.temp).then(res => {
            this.$notify({
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
            this.dialogFormVisible = false
            this.$refs.dataTable.getList()
          })
        }
      })
    },
    handleUpdate(row) {
      this.isNew = false
      this.dialogFormVisible = true
      this.temp = Object.assign({}, row)
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          update(this.temp).then(res => {
            this.$notify({
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
            this.dialogFormVisible = false
            this.$refs.dataTable.getList()
          })
        }
      })
    },
    handleDelete(row) {
      remove(row.ID).then(() => {
        this.$notify({
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.$refs.dataTable.deleteData(row)
      })
    }
  }
}

</script>
